﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Lists;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Profile;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Services.Tokens;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.Web;

    public class TokenStoreReplace : BaseCustomTokenReplace
    {
        private string _Lang;
        private int _OrderID;
        private int _PortalID;

        public TokenStoreReplace()
        {
            this._PortalID = -1;
            this._Lang = "";
            this._PortalID = -1;
        }

        public TokenStoreReplace(NB_Store_CategoriesInfo objCategoryInfo)
        {
            this._PortalID = -1;
            this._Lang = "";
            if (objCategoryInfo != null)
            {
                this._PortalID = objCategoryInfo.PortalID;
                this._Lang = objCategoryInfo.Lang;
                IDictionary tokenList = this.GetTokenList(objCategoryInfo, objCategoryInfo.Lang);
                this.AddPropertySource(tokenList, "Category");
            }
        }

        public TokenStoreReplace(NB_Store_ModelInfo objModelInfo)
        {
            this._PortalID = -1;
            this._Lang = "";
            if (objModelInfo != null)
            {
                this._PortalID = objModelInfo.PortalID;
                this._Lang = objModelInfo.Lang;
                IDictionary tokenList = this.GetTokenList(objModelInfo, objModelInfo.Lang);
                this.AddPropertySource(tokenList, "Model");
            }
        }

        public TokenStoreReplace(ProductListInfo objProductInfo)
        {
            this._PortalID = -1;
            this._Lang = "";
            if (objProductInfo != null)
            {
                this._PortalID = objProductInfo.PortalID;
                this._Lang = objProductInfo.Lang;
                IDictionary tokenList = this.GetTokenList(objProductInfo, objProductInfo.Lang);
                this.AddPropertySource(tokenList, "Product");
            }
        }

        public TokenStoreReplace(int PortalID)
        {
            this._PortalID = -1;
            this._Lang = "";
            this._PortalID = PortalID;
        }

        public TokenStoreReplace(NB_Store_OrdersInfo objOrderInfo, string Lang)
        {
            this._PortalID = -1;
            this._Lang = "";
            if (objOrderInfo != null)
            {
                this._OrderID = objOrderInfo.OrderID;
                this._PortalID = objOrderInfo.PortalID;
                this._Lang = Lang;
                this.LoadStoreTokens(objOrderInfo, Lang);
            }
        }

        public TokenStoreReplace(int PortalID, string Lang)
        {
            this._PortalID = -1;
            this._Lang = "";
            this._PortalID = PortalID;
            this._Lang = Lang;
        }

        public TokenStoreReplace(CartTotals CTotals, string Lang, int PortalID = -1)
        {
            this._PortalID = -1;
            this._Lang = "";
            this._PortalID = PortalID;
            this.LoadCartTokens(CTotals, Lang);
        }

        public void AddPropertySource(IDictionary Custom, string CustomCaption)
        {
            base.PropertySource.Add(CustomCaption.ToLower(), new DictionaryPropertyAccess(Custom));
        }

        public string DoTokenReplace(string strSourceText)
        {
            return this.DoTokenReplace(strSourceText, this._Lang, false);
        }

        public string DoTokenReplace(string strSourceText, bool ClientBound)
        {
            return this.DoTokenReplace(strSourceText, this._Lang, ClientBound);
        }

        public string DoTokenReplace(string strSourceText, string Lang)
        {
            return this.DoTokenReplace(strSourceText, Lang, false);
        }

        public string DoTokenReplace(string strSourceText, string Lang, bool ClientBound)
        {
            if (this._PortalID < 0)
            {
                PortalSettings currentPortalSettings = PortalController.GetCurrentPortalSettings();
                this._PortalID = currentPortalSettings.PortalId;
            }
            bool flag = false;
            if ((Strings.InStr(strSourceText, "[[", CompareMethod.Binary) > 0) | (Strings.InStr(strSourceText, "]]", CompareMethod.Binary) > 0))
            {
                strSourceText = Strings.Replace(strSourceText, "[[", "**SQROPEN**", 1, -1, CompareMethod.Binary);
                strSourceText = Strings.Replace(strSourceText, "]]", "**SQRCLOSE**", 1, -1, CompareMethod.Binary);
                flag = true;
            }
            if (Lang == "")
            {
                Lang = this._Lang;
            }
            strSourceText = this.replaceTemplates(strSourceText, Lang, 1);
            if (Strings.InStr(strSourceText, "[Report:", CompareMethod.Binary) > 0)
            {
                strSourceText = this.ReplaceReportTokens(this._PortalID, strSourceText);
            }
            if (Strings.InStr(strSourceText, "[WishList:ItemCount]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[WishList:ItemCount]", WishList.GetItemCount(this._PortalID), 1, -1, CompareMethod.Binary);
            }
            if (Strings.InStr(strSourceText, "[Order:GoogleEcommerceUpdate]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[Order:GoogleEcommerceUpdate]", this.getGoogleAnalyticsEcommerceScript(this._PortalID), 1, -1, CompareMethod.Binary);
            }
            if (Strings.InStr(strSourceText, "[Setting:", CompareMethod.Binary) > 0)
            {
                strSourceText = this.ReplaceSettingTokens(this._PortalID, strSourceText);
            }
            if (Strings.InStr(strSourceText, "[Order:DetailRows]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[Order:DetailRows]", this.getCartDetailHtmlRows(), 1, -1, CompareMethod.Binary);
            }
            if (Strings.InStr(strSourceText, "[Order:DetailTemplateRows]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[Order:DetailTemplateRows]", this.getCartDetailTemplateRows("", ClientBound), 1, -1, CompareMethod.Binary);
            }
            if (Strings.InStr(strSourceText, "[Order:TrackingLink]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[Order:TrackingLink]", this.getTrackingLink(), 1, -1, CompareMethod.Binary);
            }
            if (Strings.InStr(strSourceText, "[Order:ShippingMethod]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[Order:ShippingMethod]", this.getShippingMethod(), 1, -1, CompareMethod.Binary);
            }
            if (Strings.InStr(strSourceText, "[Currency:Symbol]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[Currency:Symbol]", SharedFunctions.getCurrencySymbol(), 1, -1, CompareMethod.Binary);
            }
            if ((Strings.InStr(strSourceText, "[Cart:URL]", CompareMethod.Binary) > 0) && Versioned.IsNumeric(SharedFunctions.GetStoreSetting(this._PortalID, "checkout.tab")))
            {
                strSourceText = Strings.Replace(strSourceText, "[Cart:URL]", DotNetNuke.Common.Globals.NavigateURL(Conversions.ToInteger(SharedFunctions.GetStoreSetting(this._PortalID, "checkout.tab"))), 1, -1, CompareMethod.Binary);
            }
            if (Strings.InStr(strSourceText, "[Cart:TabID]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[Cart:TabID]", SharedFunctions.GetStoreSetting(this._PortalID, "checkout.tab"), 1, -1, CompareMethod.Binary);
            }
            if (Strings.InStr(strSourceText, "[Order:AmendedLink]", CompareMethod.Binary) > 0)
            {
                strSourceText = Strings.Replace(strSourceText, "[Order:AmendedLink]", DotNetNuke.Common.Globals.NavigateURL(SharedFunctions.GetStoreSettingInt(this._PortalID, "checkout.tab", SharedFunctions.GetCurrentCulture()), "", new string[] { "codeid=[Order:OrderGUID]" }), 1, -1, CompareMethod.Binary);
            }
            if (TokenExtraInterface.Instance() != null)
            {
                strSourceText = TokenExtraInterface.Instance().DoExtraReplace(this._PortalID, strSourceText, this._Lang, this._OrderID);
            }
            string expression = this.ReplaceTokens(strSourceText);
            if (flag)
            {
                expression = Strings.Replace(Strings.Replace(expression, "**SQROPEN**", "[", 1, -1, CompareMethod.Binary), "**SQRCLOSE**", "]", 1, -1, CompareMethod.Binary);
            }
            return expression;
        }

        private string getCartDetailHtmlRows()
        {
            string str2 = "";
            OrderController controller = new OrderController();
            NB_Store_OrdersInfo order = controller.GetOrder(this._OrderID);
            if (order != null)
            {
                IEnumerator enumerator;
                bool flag = SharedFunctions.GetStoreSettingBoolean(order.PortalID, "productlinksoncart.flag", SharedFunctions.GetCurrentCulture());
                ArrayList orderDetailList = controller.GetOrderDetailList(this._OrderID);
                try
                {
                    enumerator = orderDetailList.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        NB_Store_OrderDetailsInfo current = (NB_Store_OrderDetailsInfo) enumerator.Current;
                        str2 = str2 + "<tr class=\"OrderDetailRow\">";
                        str2 = str2 + "<td>";
                        if (flag)
                        {
                            str2 = str2 + "<a href=\"" + current.ProductURL + "\" target=\"_blank\" >" + current.ItemDesc + "</a>";
                        }
                        else
                        {
                            str2 = str2 + current.ItemDesc;
                        }
                        str2 = str2 + "</td>";
                        str2 = str2 + "<td align=\"center\">";
                        str2 = str2 + SharedFunctions.FormatToStoreCurrency(Convert.ToDouble(current.UnitCost));
                        str2 = str2 + "</td>";
                        str2 = str2 + "<td align=\"center\">";
                        str2 = str2 + Conversions.ToString(current.Quantity);
                        str2 = str2 + "</td>";
                        str2 = str2 + "<td  align=\"right\">";
                        str2 = str2 + SharedFunctions.FormatToStoreCurrency(Convert.ToDouble(decimal.Multiply(current.UnitCost, new decimal(current.Quantity))));
                        str2 = str2 + "</td>";
                        str2 = str2 + "</tr>";
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            return str2;
        }

        private string getCartDetailTemplateRows(string TemplateName, bool ClientBound)
        {
            string str4 = "";
            OrderController controller = new OrderController();
            ProductController controller2 = new ProductController();
            string expression = "";
            if (TemplateName == "")
            {
                TemplateName = "detailrows.template";
            }
            string str = SharedFunctions.GetStoreSettingText(this._PortalID, TemplateName, this._Lang);
            if (str == "")
            {
                str = "*** No cart template: detailrows.template ****";
            }
            if (controller.GetOrder(this._OrderID) != null)
            {
                IEnumerator enumerator;
                ArrayList orderDetailList = controller.GetOrderDetailList(this._OrderID);
                try
                {
                    enumerator = orderDetailList.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        NB_Store_OrderDetailsInfo current = (NB_Store_OrderDetailsInfo) enumerator.Current;
                        TokenStoreReplace replace = new TokenStoreReplace(this._PortalID, this._Lang);
                        replace.AddPropertySource(replace.GetTokenList(current, this._Lang), "Detail");
                        NB_Store_ModelInfo model = controller2.GetModel(current.ModelID, this._Lang);
                        if (model != null)
                        {
                            replace.AddPropertySource(replace.GetTokenList(model, this._Lang), "Model");
                        }
                        ProductListInfo productListInfo = controller2.GetProductListInfo(model.ProductID, this._Lang);
                        if (productListInfo != null)
                        {
                            replace.AddPropertySource(replace.GetTokenList(productListInfo, this._Lang), "Product");
                        }
                        expression = str;
                        if (current.CartXMLInfo != "")
                        {
                            UserInfo currentUserInfo = UserController.GetCurrentUserInfo();
                            if ((currentUserInfo != null) && (SharedFunctions.IsEditor(this._PortalID, currentUserInfo) & !ClientBound))
                            {
                                expression = Strings.Replace(str, "[Detail:FileLink]", "<a href='/DesktopModules/NB_Store/FileProvider.ashx?did=" + current.OrderDetailID.ToString() + "'><img src='/DesktopModules/NB_Store/img/file.gif'/></a>", 1, -1, CompareMethod.Binary);
                            }
                        }
                        expression = Strings.Replace(expression, "[Detail:FileLink]", "", 1, -1, CompareMethod.Binary);
                        str4 = str4 + HttpUtility.HtmlDecode(replace.DoTokenReplace(expression));
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            return str4;
        }

        private string getGoogleAnalyticsEcommerceScript(int PortalID)
        {
            object obj3;
            IEnumerator enumerator2;
            string str2 = "";
            if (SharedFunctions.IsDNN4())
            {
                return str2;
            }
            string str3 = string.Empty;
            try
            {
                obj3 = RuntimeHelpers.GetObjectValue(Activator.CreateInstance("DotNetNuke", "DotNetNuke.Services.Analytics.Config.AnalyticsConfiguration").Unwrap());
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                ProjectData.ClearProjectError();
                return "";
            }
            object objectValue = RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(obj3, null, "GetConfig", new object[] { "GoogleAnalytics" }, null, null, null));
            if (objectValue != null)
            {
                IEnumerator enumerator;
                string str4 = string.Empty;
                try
                {
                    enumerator = ((IEnumerable) NewLateBinding.LateGet(objectValue, null, "Settings", new object[0], null, null, null)).GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        object instance = RuntimeHelpers.GetObjectValue(enumerator.Current);
                        object left = NewLateBinding.LateGet(NewLateBinding.LateGet(instance, null, "SettingName", new object[0], null, null, null), null, "ToLower", new object[0], null, null, null);
                        if (Operators.ConditionalCompareObjectEqual(left, "trackingid", false))
                        {
                            str3 = Conversions.ToString(NewLateBinding.LateGet(instance, null, "SettingValue", new object[0], null, null, null));
                        }
                        else if (Operators.ConditionalCompareObjectEqual(left, "urlparameter", false))
                        {
                            str4 = Conversions.ToString(NewLateBinding.LateGet(instance, null, "SettingValue", new object[0], null, null, null));
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            if (str3 == string.Empty)
            {
                return string.Empty;
            }
            OrderController controller2 = new OrderController();
            NB_Store_OrdersInfo order = controller2.GetOrder(this._OrderID);
            if (order == null)
            {
                return string.Empty;
            }
            NB_Store_AddressInfo orderAddress = new OrderController().GetOrderAddress(order.ShippingAddressID);
            if (orderAddress == null)
            {
                return string.Empty;
            }
            StringWriter writer = new StringWriter();
            writer.WriteLine("<script type=\"text/javascript\">");
            writer.WriteLine("var _gaq = _gaq || [");
            writer.WriteLine("];");
            writer.WriteLine("_gaq.push(['_setAccount', '" + str3 + "']);");
            writer.WriteLine("_gaq.push(['_trackPageview']);");
            writer.WriteLine("_gaq.push(['_addTrans',");
            writer.WriteLine("'" + Conversions.ToString(order.OrderID) + "',");
            writer.WriteLine("'" + SharedFunctions.GetStoreSetting(PortalID, "store.name") + "',");
            writer.WriteLine("'" + Conversions.ToString(order.Total) + "',");
            writer.WriteLine("'" + Conversions.ToString(order.ShippingCost) + "',");
            writer.WriteLine("'" + orderAddress.City + "',");
            writer.WriteLine("'" + orderAddress.RegionCode + "',");
            writer.WriteLine("'" + orderAddress.CountryCode + "'");
            writer.WriteLine("]);");
            ProductController controller3 = new ProductController();
            ArrayList orderDetailList = controller2.GetOrderDetailList(this._OrderID);
            try
            {
                enumerator2 = orderDetailList.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    NB_Store_OrderDetailsInfo current = (NB_Store_OrderDetailsInfo) enumerator2.Current;
                    NB_Store_ModelInfo model = controller3.GetModel(current.ModelID, this._Lang);
                    NB_Store_ProductsInfo product = controller3.GetProduct(model.ProductID, this._Lang);
                    writer.WriteLine("_gaq.push(['_addItem',");
                    writer.WriteLine("'" + Conversions.ToString(this._OrderID) + "',");
                    writer.WriteLine("'" + model.Barcode + "',");
                    writer.WriteLine("'" + product.ProductName.Replace("'", "").Replace("\"", "") + "',");
                    writer.WriteLine("'" + model.ModelName.Replace("'", "").Replace("\"", "") + "',");
                    writer.WriteLine("'" + Conversions.ToString(current.UnitCost) + "',");
                    writer.WriteLine("'" + Conversions.ToString(current.Quantity) + "'");
                    writer.WriteLine("]);");
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            writer.WriteLine("_gaq.push(['_trackTrans']);");
            writer.WriteLine("(function() {");
            writer.WriteLine("var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
            writer.WriteLine("ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
            writer.WriteLine("var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
            writer.WriteLine("})();");
            writer.WriteLine("</script>");
            return writer.ToString();
        }

        private IDictionary GetProfileTokensList(UserInfo obj, string Lang)
        {
            IEnumerator enumerator;
            IDictionary<string, string> dictionary2 = new Dictionary<string, string>();
            try
            {
                enumerator = obj.Profile.ProfileProperties.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ProfilePropertyDefinition current = (ProfilePropertyDefinition) enumerator.Current;
                    if (!dictionary2.ContainsKey(current.PropertyName))
                    {
                        dictionary2.Add(current.PropertyName, current.PropertyValue);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return (IDictionary) dictionary2;
        }

        private string getShippingMethod()
        {
            string str2 = "";
            OrderController controller = new OrderController();
            SettingsController controller2 = new SettingsController();
            ShipController controller3 = new ShipController();
            NB_Store_ShippingMethodInfo shippingMethod = null;
            NB_Store_SettingsTextInfo info3 = null;
            NB_Store_OrdersInfo order = controller.GetOrder(this._OrderID);
            if (order == null)
            {
                return str2;
            }
            shippingMethod = controller3.GetShippingMethod(order.ShipMethodID);
            if (shippingMethod == null)
            {
                return str2;
            }
            info3 = controller2.GetSettingsText(order.PortalID, shippingMethod.TemplateName, this._Lang);
            if (info3 != null)
            {
                return Strings.Replace(HttpUtility.HtmlDecode(info3.SettingText), "[TAG:SHIPPINGCOST]", SharedFunctions.FormatToStoreCurrency(Convert.ToDouble(order.ShippingCost)), 1, -1, CompareMethod.Binary);
            }
            return shippingMethod.MethodDesc;
        }

        private IDictionary GetTokenList(object obj, string Lang)
        {
            IDictionary<string, string> dictionary2 = new Dictionary<string, string>();
            Regex regex = new Regex(@"\s*");
            PropertyInfo[] properties = obj.GetType().GetProperties();
            CultureInfo provider = new CultureInfo(Lang);
            if (provider == null)
            {
                provider = Thread.CurrentThread.CurrentCulture;
            }
            foreach (PropertyInfo info2 in properties)
            {
                try
                {
                    string name = info2.PropertyType.Name;
                    if (name == "Decimal")
                    {
                        dictionary2.Add(info2.Name, SharedFunctions.FormatToStoreCurrency(Convert.ToDouble(Conversions.ToDecimal(info2.GetValue(RuntimeHelpers.GetObjectValue(obj), null)))));
                        dictionary2.Add("#" + info2.Name, regex.Replace(Strings.Replace(SharedFunctions.FormatToStoreCurrency(Convert.ToDouble(Conversions.ToDecimal(info2.GetValue(RuntimeHelpers.GetObjectValue(obj), null)))), SharedFunctions.getCurrencySymbol(), "", 1, -1, CompareMethod.Binary), ""));
                        dictionary2.Add("." + info2.Name, Conversions.ToDecimal(info2.GetValue(RuntimeHelpers.GetObjectValue(obj), null)).ToString("0.00"));
                        dictionary2.Add("=" + info2.Name, Strings.Replace(Conversions.ToDecimal(info2.GetValue(RuntimeHelpers.GetObjectValue(obj), null)).ToString("0.00"), ".", "", 1, -1, CompareMethod.Binary));
                    }
                    else if (name == "DateTime")
                    {
                        if (this._PortalID >= 0)
                        {
                            DateTime time;
                            string format = SharedFunctions.GetStoreSetting(this._PortalID, "datedisplay.format", Lang);
                            if (format != "")
                            {
                                time = (DateTime) info2.GetValue(RuntimeHelpers.GetObjectValue(obj), null);
                                dictionary2.Add(info2.Name, time.ToString(format, provider));
                            }
                            else
                            {
                                time = (DateTime) info2.GetValue(RuntimeHelpers.GetObjectValue(obj), null);
                                dictionary2.Add(info2.Name, time.ToString("d", provider));
                            }
                        }
                        else
                        {
                            dictionary2.Add(info2.Name, ((DateTime) info2.GetValue(RuntimeHelpers.GetObjectValue(obj), null)).ToString("d", provider));
                        }
                    }
                    else
                    {
                        dictionary2.Add(info2.Name, info2.GetValue(RuntimeHelpers.GetObjectValue(obj), null).ToString());
                    }
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    ProjectData.ClearProjectError();
                }
            }
            return (IDictionary) dictionary2;
        }

        private string getTrackingLink()
        {
            string str2 = "";
            OrderController controller = new OrderController();
            SettingsController controller2 = new SettingsController();
            ShipController controller3 = new ShipController();
            NB_Store_OrdersInfo order = controller.GetOrder(this._OrderID);
            if (order != null)
            {
                if (order.TrackingCode == "")
                {
                    return "";
                }
                NB_Store_ShippingMethodInfo shippingMethod = controller3.GetShippingMethod(order.ShipMethodID);
                NB_Store_SettingsTextInfo info3 = controller2.GetSettingsText(order.PortalID, shippingMethod.URLtracker, this._Lang);
                if (info3 != null)
                {
                    str2 = Strings.Replace(HttpUtility.HtmlDecode(info3.SettingText), "[TAG:TRACKINGCODE]", order.TrackingCode, 1, -1, CompareMethod.Binary);
                }
                return str2;
            }
            return "";
        }

        private void LoadCartTokens(CartTotals CTotals, string Lang)
        {
            if (CTotals != null)
            {
                IDictionary tokenList = this.GetTokenList(CTotals, Lang);
                this.AddPropertySource(tokenList, "Cart");
            }
        }

        private void LoadStoreTokens(NB_Store_OrdersInfo objOrderInfo, string Lang)
        {
            if (objOrderInfo != null)
            {
                ListEntryInfo info;
                OrderController controller = new OrderController();
                CountryLists lists = new CountryLists();
                IDictionary tokenList = this.GetTokenList(objOrderInfo, Lang);
                tokenList["Stg2FormXML"] = SharedFunctions.XSLTransByTemplate(Conversions.ToString(tokenList["Stg2FormXML"]), "Stg2FormXML.xsl");
                tokenList["Stg3FormXML"] = SharedFunctions.XSLTransByTemplate(Conversions.ToString(tokenList["Stg3FormXML"]), "Stg3FormXML.xsl");
                this.AddPropertySource(tokenList, "Order");
                NB_Store_AddressInfo orderAddress = controller.GetOrderAddress(objOrderInfo.BillingAddressID);
                if (orderAddress != null)
                {
                    tokenList = this.GetTokenList(orderAddress, Lang);
                    info = lists.getCountryEntryInfo(orderAddress.CountryCode, Lang);
                    if (info != null)
                    {
                        tokenList.Add("Country", info.Text);
                    }
                    this.AddPropertySource(tokenList, "BAddress");
                }
                orderAddress = controller.GetOrderAddress(objOrderInfo.ShippingAddressID);
                if (orderAddress != null)
                {
                    tokenList = this.GetTokenList(orderAddress, Lang);
                    info = lists.getCountryEntryInfo(orderAddress.CountryCode, Lang);
                    if (info != null)
                    {
                        tokenList.Add("Country", info.Text);
                    }
                    this.AddPropertySource(tokenList, "SAddress");
                }
                NB_Store_OrderStatusInfo orderStatus = controller.GetOrderStatus(objOrderInfo.OrderStatusID, Lang);
                if (orderStatus != null)
                {
                    tokenList = this.GetTokenList(orderStatus, Lang);
                    this.AddPropertySource(tokenList, "OrderStatus");
                }
                UserInfo user = new UserController().GetUser(objOrderInfo.PortalID, objOrderInfo.UserID);
                if (user != null)
                {
                    tokenList = this.GetTokenList(user, Lang);
                    this.AddPropertySource(tokenList, "OrderUser");
                    tokenList = this.GetProfileTokensList(user, Lang);
                    this.AddPropertySource(tokenList, "OrderUserProfile");
                }
            }
        }

        private string[] ParseTemplateText(string TemplText)
        {
            char[] separator = new char[] { '[', ']' };
            return TemplText.Split(separator);
        }

        private string ReplaceReportTokens(int PortalID, string SourceText)
        {
            string expression = SourceText;
            SQLReportController controller = new SQLReportController();
            foreach (string str3 in this.ParseTemplateText(SourceText))
            {
                if (str3.ToLower().StartsWith("report:"))
                {
                    string repRef = Strings.Replace(str3.ToLower(), "report:", "", 1, -1, CompareMethod.Binary);
                    expression = Strings.Replace(expression, "[" + str3 + "]", controller.GetReportOutput(PortalID, repRef), 1, -1, CompareMethod.Binary);
                }
            }
            return expression;
        }

        private string ReplaceSettingTokens(int PortalID, string SourceText)
        {
            string expression = SourceText;
            foreach (string str3 in this.ParseTemplateText(SourceText))
            {
                if (str3.ToLower().StartsWith("setting:"))
                {
                    string settingName = Strings.Replace(str3.ToLower(), "setting:", "", 1, -1, CompareMethod.Binary);
                    expression = Strings.Replace(expression, "[" + str3 + "]", SharedFunctions.GetStoreSetting(PortalID, settingName), 1, -1, CompareMethod.Binary);
                }
            }
            return expression;
        }

        internal string replaceTemplates(string sourceText, string Lang, int RecursiveCount = 1)
        {
            string[] strArray = this.ParseTemplateText(sourceText);
            if ((strArray.Length > 0) & (RecursiveCount < 5))
            {
                SettingsController controller = new SettingsController();
                string s = "";
                bool flag = false;
                string role = "";
                int upperBound = strArray.GetUpperBound(0);
                for (int i = 0; i <= upperBound; i++)
                {
                    if (strArray[i].ToUpper().StartsWith("TEMPLATE:"))
                    {
                        string[] strArray2 = strArray[i].Split(new char[] { ':' });
                        string settingName = strArray2[1];
                        if (strArray2.GetUpperBound(0) >= 2)
                        {
                            role = strArray2[2];
                        }
                        s = "";
                        if ((role == "") | UserController.GetCurrentUserInfo().IsInRole(role))
                        {
                            NB_Store_SettingsTextInfo info = controller.GetSettingsTextNotCached(this._PortalID, settingName, Lang);
                            if (info != null)
                            {
                                s = info.SettingText;
                            }
                            else
                            {
                                NB_Store_SettingsInfo info2 = controller.GetSettingNotCached(this._PortalID, settingName, Lang);
                                if (info2 != null)
                                {
                                    s = info2.SettingValue;
                                }
                            }
                            s = HttpUtility.HtmlDecode(s);
                        }
                        sourceText = Strings.Replace(sourceText, "[" + strArray[i] + "]", s, 1, -1, CompareMethod.Binary);
                        flag = true;
                    }
                }
                if (flag)
                {
                    sourceText = this.replaceTemplates(sourceText, Lang, RecursiveCount + 1);
                }
            }
            return sourceText;
        }

        protected override string ReplaceTokens(string strSourceText)
        {
            return base.ReplaceTokens(strSourceText);
        }
    }
}

